﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace QuanLyDoiXe
{
    public partial class frmBaoCaoSuDungNhienLieuThang : Form
    {
        public frmBaoCaoSuDungNhienLieuThang()
        {
            InitializeComponent();
        }

        private string KiemTraDuLieu()
        {
            string chuoiloi = "";

            int x;
            if (Int32.TryParse(txtNam.Text.Trim(), out x) == false)
            {
                chuoiloi += "Năm phải là một số nguyên.\n";
            }
            return chuoiloi;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            string chuoiloi = KiemTraDuLieu();
            if (chuoiloi != "")
            {
                MessageBox.Show(chuoiloi);
                return;
            }
            string @thang = cboThang.SelectedItem.ToString();
            string @nam = txtNam.Text.Trim();

            string strQuery = "SELECT t.BienSoXe, t.tongxangtieuthu, t.quangduong, cn.dinhmuc ";
            strQuery += "FROM ((SELECT q.BienSoXe, SUM(c.XangTieuThu) AS tongxangtieuthu, SUM(c.SoKmChay) AS quangduong FROM (QUANG_DUONG_XE_CHAY q INNER JOIN CHI_TIET_QUANG_DUONG_XE_CHAY c ON q.MaQDXC = c.MaQDXC)where (datepart('m',q.Ngaychay) = '" + @thang + "') AND (datepart('yyyy',q.Ngaychay) = '" + @nam + "') GROUP BY q.BienSoXe) t INNER JOIN (SELECT  BienSoXe, SUM(DinhMuc) AS dinhmuc FROM  CHI_TIET_CAP_NHIEN_LIEU  ct inner join cap_nhien_lieu cnl on ct.macapnl = cnl.macapnl where (datepart('m',cnl.Ngaycap) = '" + @thang + "') AND (datepart('yyyy',cnl.Ngaycap) = '" + @nam + "')GROUP BY BienSoXe) cn ON cn.BienSoXe = t.BienSoXe)";

            string strCnn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=QuanLyDoiXe.accdb";
            OleDbDataAdapter da = new OleDbDataAdapter(strQuery, strCnn);
            QuanLyDoiXeDataset qldx = new QuanLyDoiXeDataset();
            
            da.Fill(qldx, "SuDungNhienLieuThang");

            rptBaoCaoSuDungNhienLieuThang rptSuDungNhienLieu = new rptBaoCaoSuDungNhienLieuThang();
            rptSuDungNhienLieu.SetDataSource(qldx);
            rptSuDungNhienLieu.SetParameterValue("@thang", @thang);
            rptSuDungNhienLieu.SetParameterValue("@nam", @nam);
            rptViewerSuDungNhienLieuThang.ReportSource = rptSuDungNhienLieu;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmBaoCaoSuDungNhienLieuThang_Load(object sender, EventArgs e)
        {
            btnDongY.Enabled = false;
            cboThang.SelectedIndex = 0;
        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            if (txtNam.Text.Trim() != "")
            {
                btnDongY.Enabled = true;
            }
            else
            {
                btnDongY.Enabled = false;
            }
        }
    }
}
